﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
///PromotionDao 的摘要说明
/// </summary>
public class PromotionDao:IPromotionDao
{
	public PromotionDao()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    #region IPromotionDao 成员

    public IList<Promotion> GetPromotionList()
    {
        DataSet ds = SqlOpHelper.Query("niyi", new Object[] {  }, new Object[] {  }, "select * from Promotion");
        if (ds.Tables.Count > 0)
            return Promotion.ConvertToPromotionList(ds.Tables[0]);
        else
            return null;
    }

    public IList<Promotion> GetOrderedPromotionList()
    {
        List<Promotion> promotionList = new List<Promotion>();

        DataSet ds = SqlOpHelper.Query("niyi", new Object[] { }, new Object[] { }, "select * from Promotion where StartDay<GetDate() and DueDay>GetDate() Order by DueDay Asc");
        if (ds.Tables.Count > 0)
            promotionList.AddRange(Promotion.ConvertToPromotionList(ds.Tables[0]));

        ds = SqlOpHelper.Query("niyi", new Object[] { }, new Object[] { }, "select * from Promotion where StartDay>GetDate() Order by StartDay Asc");
        if (ds.Tables.Count > 0)
            promotionList.AddRange(Promotion.ConvertToPromotionList(ds.Tables[0]));

        ds = SqlOpHelper.Query("niyi", new Object[] { }, new Object[] { }, "select * from Promotion where DueDay<GetDate() Order by DueDay Desc");
        if (ds.Tables.Count > 0)
            promotionList.AddRange(Promotion.ConvertToPromotionList(ds.Tables[0]));
        return promotionList;

    }

    public Promotion GetPromotionById(string promotionId)
    {
        DataSet ds = SqlOpHelper.Query("niyi", new Object[] { "promotionId" }, new Object[] { promotionId }, "select * from Promotion where PromotionId = @promotionId");
        if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            return Promotion.ConvertToPromotion(ds.Tables[0].Rows[0]);
        else
            return null;
    }

    public void AddPromotion(Promotion promotion)
    {
        SqlOpHelper.Execute("niyi", new Object[] { "promotionTitle", "startDay", "dueDay", "pictureAName", "pictureBName" },
            new Object[] { promotion.PromotionTitle, promotion.StartDay, promotion.DueDay, promotion.PictureAName, promotion.PictureBName },
            "Insert Into Promotion (PromotionTitle,StartDay,DueDay,PictureAName,PictureBName)VALUES(@promotionTitle,@startDay,@dueDay,@pictureAName,@pictureBName)");
    }

    public void DeletePromotion(string promotionId)
    {
        SqlOpHelper.Execute("niyi", new Object[] { "promotionId" }, new Object[] { promotionId }, "Delete from Promotion where PromotionId = @promotionId");
    }

    public void UpdatePromotion(Promotion promtion)
    {
        throw new NotImplementedException();
    }

    #endregion
}
